home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPTernaryNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-10  |  2.0 KB  |  91 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. /////////////////////////////////////////////////////////////
  6. //Only when compiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. /////////////////////////////////////////////////////////////
  10.  
  11. #include "CGPTernaryNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. extern CGPNode *pGPCreateRandomSubtree(void);
  17.  
  18. extern CGPNode **pPrototypeList;
  19. extern unsigned long ulNumberOfPrototypes;
  20.  
  21. CGPTernaryNode::CGPTernaryNode()
  22. {
  23. }
  24.  
  25. CGPTernaryNode::~CGPTernaryNode()
  26. {
  27.     unsigned long i;
  28.     if(pChildren!=NULL)
  29.     {
  30.         for(i=0;i<ulNumberOfChildren;i++)
  31.         {
  32.             if(pChildren[i]!=NULL)
  33.             {
  34.                 delete pChildren[i];
  35.             }
  36.         }
  37.         delete []pChildren;
  38.     }
  39. }
  40.  
  41. CGPNode *CGPTernaryNode::pGetCopy(CGP* pGP)
  42. {
  43.     unsigned long i;
  44.     CGPTernaryNode *pNewNode=new CGPTernaryNode;
  45.     assert(!(pNewNode==NULL));
  46.     for(i=0;i<ulNumberOfChildren;i++)
  47.     {
  48.         if(pNewNode->pChildren[i]!=NULL)
  49.         {
  50.             delete pNewNode->pChildren[i];
  51.         }
  52.         if(!nIsPrototype)
  53.         {
  54.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  55.         }
  56.         else
  57.         {
  58.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  59.         }
  60.     }
  61.     return (CGPNode*)pNewNode;
  62. }
  63.  
  64. unsigned long CGPTernaryNode::ulGetNumberOfNodesInSubtree(unsigned long ulNodesFoundSoFar)
  65. {
  66.     return ulNodesFoundSoFar+3+pChildren[0]->ulGetNumberOfNodesInSubtree(0)+pChildren[1]->ulGetNumberOfNodesInSubtree(0)+pChildren[2]->ulGetNumberOfNodesInSubtree(0);
  67. }
  68.  
  69. void CGPTernaryNode::GetnthNode(unsigned long& ulNodesSoFar,unsigned long ulTargetNode,CGPNode **&pTheNode)
  70. {
  71.     if(pTheNode==NULL)
  72.     {
  73.         if(ulTargetNode==ulNodesSoFar)
  74.         {
  75.             pTheNode=&pChildren[0];
  76.         }
  77.         if(ulTargetNode==ulNodesSoFar+1)
  78.         {
  79.             pTheNode=&pChildren[1];
  80.         }
  81.         if(ulTargetNode==ulNodesSoFar+2)
  82.         {
  83.             pTheNode=&pChildren[2];
  84.         }
  85.     }
  86.     ulNodesSoFar+=3;
  87.     pChildren[0]->GetnthNode(ulNodesSoFar,ulTargetNode,pTheNode);
  88.     pChildren[1]->GetnthNode(ulNodesSoFar,ulTargetNode,pTheNode);
  89.     pChildren[2]->GetnthNode(ulNodesSoFar,ulTargetNode,pTheNode);
  90. }
  91.